package com.base.common.util;

/**
 * 计时器，封装了Apache的StopWatch. 添加了记录两次split之间的时间间隔
 * 
 * @author yanjiaqiang
 * @date 2010-10-12
 * @version $Id: StopTimer.java 7 2011-07-15 04:24:04Z JiaqiangYan@gmail.com $
 */
public class StopTimer {

	/**
	 * 上一次split()到start()之间的时间间隔
	 */
	private long lastSplittime;
	/**
	 * 上次split和上上次split的时间间隔。
	 */
	private long intervalSplittime;

	/**
	 * 上次split和上上次split的时间间隔。
	 * 
	 * @return
	 */
	public long getIntervalSplitTime() {
		return intervalSplittime;
	}

	// running states
	private static final int STATE_UNSTARTED = 0;

	private static final int STATE_RUNNING = 1;

	private static final int STATE_STOPPED = 2;

	private static final int STATE_SUSPENDED = 3;

	// split state
	private static final int STATE_UNSPLIT = 10;

	private static final int STATE_SPLIT = 11;

	/**
	 * The current running state of the StopWatch.
	 */
	private int runningState = STATE_UNSTARTED;

	/**
	 * Whether the stopwatch has a split time recorded.
	 */
	private int splitState = STATE_UNSPLIT;

	/**
	 * The start time.
	 */
	private long startTime = -1;

	/**
	 * The stop time.
	 */
	private long stopTime = -1;

	/**
	 * <p>
	 * Start the stopwatch.
	 * </p>
	 * 
	 * <p>
	 * This method starts a new timing session, clearing any previous values.
	 * </p>
	 * 
	 * @throws IllegalStateException
	 *             if the StopWatch is already running.
	 */
	public void start() {
		if (this.runningState == STATE_STOPPED) {
			throw new IllegalStateException("Stopwatch must be reset before being restarted. ");
		}
		if (this.runningState != STATE_UNSTARTED) {
			throw new IllegalStateException("Stopwatch already started. ");
		}
		this.stopTime = -1;
		this.startTime = System.currentTimeMillis();
		this.runningState = STATE_RUNNING;
	}

	/**
	 * <p>
	 * Stop the stopwatch.
	 * </p>
	 * 
	 * <p>
	 * This method ends a new timing session, allowing the time to be retrieved.
	 * </p>
	 * 
	 * @throws IllegalStateException
	 *             if the StopWatch is not running.
	 */
	public void stop() {
		if (this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) {
			throw new IllegalStateException("Stopwatch is not running. ");
		}
		if (this.runningState == STATE_RUNNING) {
			this.stopTime = System.currentTimeMillis();
		}
		this.runningState = STATE_STOPPED;
	}

	/**
	 * <p>
	 * Resets the stopwatch. Stops it if need be.
	 * </p>
	 * 
	 * <p>
	 * This method clears the internal values to allow the object to be reused.
	 * </p>
	 */
	public void reset() {
		this.runningState = STATE_UNSTARTED;
		this.splitState = STATE_UNSPLIT;
		this.startTime = -1;
		this.stopTime = -1;
	}

	/**
	 * <p>
	 * Split the time.
	 * </p>
	 * 
	 * <p>
	 * This method sets the stop time of the watch to allow a time to be
	 * extracted. The start time is unaffected, enabling {@link #unsplit()} to
	 * continue the timing from the original start point.
	 * </p>
	 * 
	 * @throws IllegalStateException
	 *             if the StopWatch is not running.
	 */
	public void split() {
		if (this.runningState != STATE_RUNNING) {
			throw new IllegalStateException("Stopwatch is not running. ");
		}
		this.stopTime = System.currentTimeMillis();
		this.splitState = STATE_SPLIT;

		intervalSplittime = getSplitTime() - lastSplittime;
		lastSplittime = getSplitTime();
	}

	/**
	 * <p>
	 * Remove a split.
	 * </p>
	 * 
	 * <p>
	 * This method clears the stop time. The start time is unaffected, enabling
	 * timing from the original start point to continue.
	 * </p>
	 * 
	 * @throws IllegalStateException
	 *             if the StopWatch has not been split.
	 */
	public void unsplit() {
		if (this.splitState != STATE_SPLIT) {
			throw new IllegalStateException("Stopwatch has not been split. ");
		}
		this.stopTime = -1;
		this.splitState = STATE_UNSPLIT;
	}

	/**
	 * <p>
	 * Suspend the stopwatch for later resumption.
	 * </p>
	 * 
	 * <p>
	 * This method suspends the watch until it is resumed. The watch will not
	 * include time between the suspend and resume calls in the total time.
	 * </p>
	 * 
	 * @throws IllegalStateException
	 *             if the StopWatch is not currently running.
	 */
	public void suspend() {
		if (this.runningState != STATE_RUNNING) {
			throw new IllegalStateException("Stopwatch must be running to suspend. ");
		}
		this.stopTime = System.currentTimeMillis();
		this.runningState = STATE_SUSPENDED;
	}

	/**
	 * <p>
	 * Resume the stopwatch after a suspend.
	 * </p>
	 * 
	 * <p>
	 * This method resumes the watch after it was suspended. The watch will not
	 * include time between the suspend and resume calls in the total time.
	 * </p>
	 * 
	 * @throws IllegalStateException
	 *             if the StopWatch has not been suspended.
	 */
	public void resume() {
		if (this.runningState != STATE_SUSPENDED) {
			throw new IllegalStateException("Stopwatch must be suspended to resume. ");
		}
		this.startTime += (System.currentTimeMillis() - this.stopTime);
		this.stopTime = -1;
		this.runningState = STATE_RUNNING;
	}

	/**
	 * <p>
	 * Get the time on the stopwatch.
	 * </p>
	 * 
	 * <p>
	 * This is either the time between the start and the moment this method is
	 * called, or the amount of time between start and stop.
	 * </p>
	 * 
	 * @return the time in milliseconds
	 */
	public long getTime() {
		if (this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) {
			return this.stopTime - this.startTime;
		} else if (this.runningState == STATE_UNSTARTED) {
			return 0;
		} else if (this.runningState == STATE_RUNNING) {
			return System.currentTimeMillis() - this.startTime;
		}
		throw new RuntimeException("Illegal running state has occured. ");
	}

	/**
	 * <p>
	 * Get the split time on the stopwatch.
	 * </p>
	 * 
	 * <p>
	 * This is the time between start and latest split.
	 * </p>
	 * 
	 * @return the split time in milliseconds
	 * 
	 * @throws IllegalStateException
	 *             if the StopWatch has not yet been split.
	 * @since 2.1
	 */
	public long getSplitTime() {
		if (this.splitState != STATE_SPLIT) {
			throw new IllegalStateException("Stopwatch must be split to get the split time. ");
		}
		return this.stopTime - this.startTime;
	}

	/**
	 * Returns the time this stopwatch was started.
	 * 
	 * @return the time this stopwatch was started
	 * @throws IllegalStateException
	 *             if this StopWatch has not been started
	 * @since 2.4
	 */
	public long getStartTime() {
		if (this.runningState == STATE_UNSTARTED) {
			throw new IllegalStateException("Stopwatch has not been started");
		}
		return this.startTime;
	}

}
